package nl.hanze.shoestring.logic;

import java.awt.Component;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import nl.hanze.shoestring.domain.Planning;

/**
 * Utils: bibliotheek met diverse formatters etc.
 * 
 * @author bh
 */
public class Utils {
    
    
    public Utils() {
                
    }
       /**
     * formatteer float naar geheel getal
     * @param number
     * @return 
     */
    public static String getIntegerFromFloatNumber(Float number) {
        NumberFormat formatter = new DecimalFormat("##");
        String s =formatter.format(number);
        return s;
    }

    /**
     * formatteer getal naar float met 2 decimalen (bedrag)
     * @param number
     * @return (String) getal met twee decimalen tbv valuta
     */
    public  static String getValutaFormatFromNumber(Float number) {
        NumberFormat formatter = new DecimalFormat("#0.00");
        String s =formatter.format(number);
        return s;
    }

    /**
     * formatteer integere waarde of null uit text invoerveld
     * @param s
     * @return null of integer
     */
    public  static Integer getIntegerValueOrNull(String s) {
        Integer number;
        // opslag in amerikaanse notatie dus . ipv ,
        s = s.replace(",", ".");
        try {
            number = Integer.parseInt(s);
        }
        catch (Exception e) {
            return null;
        }
        return number;
    }

    /**
     * formatteer float waarde of null uit text invoerveld
     * @param s
     * @return null of float
     */
    public  static Float getFloatValueOrNull(String s) {
        Float number;
        // opslag in amerikaanse notatie dus . ipv ,
        s = s.replace(",", ".");
        try {
            number = Float.parseFloat(s);
        }
        catch (NumberFormatException e) {
            return null;
        }
        return number;
    }  

    /**
     * SQL datum
     * @param utilDate 
     * @return sqldatum
     */
    public  static Date getSqlDate(Date utilDate) { 
        java.util.Calendar cal = Calendar.getInstance();
        cal.setTime(utilDate);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);    
        java.sql.Date sqlDate = new java.sql.Date(cal.getTime().getTime()); // your sql date
        return sqlDate;
    }        
    
    /**
     * Verwijdert extra html tags die HTMLEditorKit worden toegevoegd
     * @param s
     * @return 
     */
     public static String removeExtraTags(String s){
        int start = s.indexOf("<body>") + 9;
        int end = s.indexOf("</body>");    
      
        return s.substring(start, end);
    }
     
    /**
     * Converteer eerste karakter van String in Hoofdletter
     * @param String s
     * @return string
     */
     public static String setFirstCharToUpper(String s){
     
        char[] stringArray = s.toCharArray();
        stringArray[0] = Character.toUpperCase(stringArray[0]);
        return new String(stringArray);
    }
    
    /**
     * Druk foutmeldingen af
     * @param comp component
     * @param emes message
     * @param title frame title
     * @
     * 
     */ 
    public static void errorMessage(Component comp, String title, String emes) {
        JOptionPane.showMessageDialog(comp, emes, title, JOptionPane.ERROR_MESSAGE);
    }
    
   /**
     * Get date of tomorrow
     *
     * 
     * @return date actual date + 1
     */
    public static Date getDateOfTomorrow() {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DATE, 1);
        return now.getTime();
    }

}
